package com.basksoft.baskreport.demo.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSourceFactory;

import com.basksoft.baskreport.demo.ToolSpring;
import com.basksoft.report.core.model.datasource.ConnectionProvider;

public class TestConnectionProvider implements ConnectionProvider {
	private static Properties properties = new Properties();
	private static DataSource dataSource;
	// 加载DBCP配置文件
	// 加载DBCP配置文件
    static {
        properties.put("username", getProperty("baskserver.sampledb.username"));
        properties.put("password", getProperty("baskserver.sampledb.password"));
        properties.put("driverClassName", getProperty("baskserver.sampledb.driverClassName"));
        properties.put("url",getProperty("baskserver.sampledb.url"));
        try {
            dataSource = BasicDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static String getProperty(String key) {
        return ToolSpring.getEnvironment().getProperty(key);
    }

	public Connection getConnection() {
		Connection connection = null;
		try {
			connection = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
}
